package com.yu.subject.application.controller;

import com.yu.subject.application.entity.SubjectCategoryDTO;
import com.yu.subject.common.entity.Result;
import com.yu.subject.common.exception.CodeAndMessage;
import com.yu.subject.common.exception.JavaSmException;
import com.yu.subject.common.util.BeanCopyUtil;
import com.yu.subject.domain.entity.SubjectCategoryBO;
import com.yu.subject.domain.service.SubjectCategoryDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 路亚
 * @version 0.1
 * @className SubjectCategoryController
 * @description
 * @date 2024/12/21 20:31
 * @since jdk11
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
@RequiredArgsConstructor
public class SubjectCategoryController {

    private final SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("新增题目分类，参数：{}", subjectCategoryDTO);
        }
        if (subjectCategoryDTO.getCategoryType() == null) {
            throw new JavaSmException(CodeAndMessage.CATEGORY_TYPE_NOT_NULL);
        }
        if (!StringUtils.isEmpty(subjectCategoryDTO.getCategoryName())) {
            throw new JavaSmException(CodeAndMessage.CATEGORY_NAME_NOT_NULL);
        }
        if (subjectCategoryDTO.getParentId() == null) {
            throw new JavaSmException(CodeAndMessage.PARENT_ID_NOT_NULL);
        }
        SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
        BeanUtils.copyProperties(subjectCategoryDTO, subjectCategoryBO);
        subjectCategoryDomainService.add(subjectCategoryBO);
        return Result.ok(true);
    }

    @GetMapping("/queryPrimaryCategory")
    public Result queryPrimaryCategory() {
        List<SubjectCategoryBO> list = subjectCategoryDomainService.queryPrimaryCategory();
        List<SubjectCategoryDTO> result = BeanCopyUtil.copyListProperties(list, SubjectCategoryDTO.class);
        return Result.ok(result);
    }

    @GetMapping("/queryCategoryByPrimary")
    public Result queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
        BeanUtils.copyProperties(subjectCategoryDTO, subjectCategoryBO);
        List<SubjectCategoryBO> list = subjectCategoryDomainService.queryCategoryByPrimary(subjectCategoryBO);
        List<SubjectCategoryDTO> result = BeanCopyUtil.copyListProperties(list, SubjectCategoryDTO.class);
        return Result.ok(result);
    }

    /**
     * 更新分类
     */
    @PostMapping("/update")
    public Result update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("更新题目分类，参数：{}", subjectCategoryDTO);
        }
        SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
        BeanUtils.copyProperties(subjectCategoryDTO, subjectCategoryBO);
        subjectCategoryDomainService.updateById(subjectCategoryBO);
        return Result.ok(true);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id){
        if (log.isInfoEnabled()){
            log.info("删除题目分类，参数：{}", id);
        }
        subjectCategoryDomainService.deleteById(id);
        return Result.ok(true);
    }
}
